package com.quectel.aiot.controller;

import com.quectel.aiot.entity.Voice;
import com.quectel.aiot.service.VoiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/voice")
@Api(tags = "语音配置管理", description = "语音配置相关接口")
public class VoiceController {
    @Autowired
    private VoiceService voiceService;

    // 查询列表
    @GetMapping("/list")
    @ApiOperation("获取语音配置列表")
    public Map<String, Object> list() {
        List<Voice> list = voiceService.getVoiceList();
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "查询成功");
        result.put("data", list);
        return result;
    }

    // 查询单个
    @GetMapping("/{id}")
    @ApiOperation("根据ID获取语音配置详情")
    public Map<String, Object> get(
            @ApiParam(name = "id", value = "语音配置ID", required = true) @PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        Voice voice = voiceService.getVoiceById(id);
        if (voice == null) {
            result.put("success", false);
            result.put("message", "声音配置不存在");
            result.put("data", null);
        } else {
            result.put("success", true);
            result.put("message", "查询成功");
            result.put("data", voice);
        }
        return result;
    }

    // 新增
    @PostMapping
    @ApiOperation("新增语音配置")
    public Map<String, Object> add(
            @ApiParam(name = "voice", value = "语音配置信息", required = true) @RequestBody Voice voice) {
        voiceService.addVoice(voice);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "声音配置新增成功");
        result.put("id", voice.getId());
        return result;
    }

    // 修改
    @PutMapping
    @ApiOperation("修改语音配置")
    public Map<String, Object> update(
            @ApiParam(name = "voice", value = "语音配置信息", required = true) @RequestBody Voice voice) {
        Map<String, Object> result = new HashMap<>();
        Voice old = voiceService.getVoiceById(voice.getId());
        if (old == null) {
            result.put("success", false);
            result.put("message", "声音配置不存在");
            return result;
        }
        int updated = voiceService.updateVoice(voice);
        if (updated > 0) {
            result.put("success", true);
            result.put("message", "声音配置更新成功");
        } else {
            result.put("success", false);
            result.put("message", "未做任何修改");
        }
        return result;
    }

    // 删除
    @DeleteMapping("/{id}")
    @ApiOperation("删除语音配置")
    public Map<String, Object> delete(
            @ApiParam(name = "id", value = "语音配置ID", required = true) @PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        Voice old = voiceService.getVoiceById(id);
        if (old == null) {
            result.put("success", false);
            result.put("message", "声音配置不存在，无法删除");
        } else {
            voiceService.deleteVoice(id);
            result.put("success", true);
            result.put("message", "声音配置删除成功");
        }
        return result;
    }
} 